package app.texas.com.devilfishhouse.View;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import com.universal_library.activity.BaseActivity;
import com.universal_library.weight.AppToast;
import com.universal_library.weight.TabWeight.BottomTab;
import com.universal_library.weight.TabWeight.BottomTabGroup;

import java.util.ArrayList;
import java.util.List;

import app.texas.com.devilfishhouse.R;
import app.texas.com.devilfishhouse.View.Fragment.home.HomeFragmentView;
import app.texas.com.devilfishhouse.View.Fragment.houuseList.HouseListFragmentView;
import app.texas.com.devilfishhouse.View.Fragment.mine.MineFragmentView;
import app.texas.com.devilfishhouse.ui.SimpleBackPage;
import app.texas.com.devilfishhouse.ui.UiHelper;

public class MainActivity extends BaseActivity {
    private static BottomTabGroup bottomTagGroup;
    private static FragmentManager mManager;
    private static FragmentTransaction mTransaction;
    private BottomTab tab_home, tab_study, tab_center, tab_findWork, tab_mine;
    private static int checkFlg = 0;
    private static List<Fragment> frags = new ArrayList<>();
    private static Fragment mFrag;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mManager = getSupportFragmentManager();
        frags.add(new HomeFragmentView());
        frags.add(new HouseListFragmentView());
        frags.add(new HouseListFragmentView());
        frags.add(new MineFragmentView());
        initTabs();
    }

    @Override
    protected void initData() {

    }

    private void initTabs() {
        bottomTagGroup = (BottomTabGroup) findViewById(R.id.bottomtabGroup);
        tab_home = (BottomTab) bottomTagGroup.getChildAt(0);
        tab_study = (BottomTab) bottomTagGroup.getChildAt(1);
        tab_center = (BottomTab) bottomTagGroup.getChildAt(2);
        tab_findWork = (BottomTab) bottomTagGroup.getChildAt(3);
        tab_mine = (BottomTab) bottomTagGroup.getChildAt(4);

//        tab_home.setHint("9");//不显示数字
        tab_home.setHintEnable(false);//不显示红点

        bottomTagGroup.setOnCheckedChangeListener(new BottomTabGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(BottomTabGroup root, int checkedId) {
                switch (checkedId) {
                    case R.id.tab_home:
                        Log.i("点击了首页", "");
                        loadFragment(0);
                        checkFlg = 0;
                        break;
                    case R.id.tab_study:
                        loadFragment(1);
                        checkFlg = 1;
                        break;
                    case R.id.tab_center:
                        checkFlg = 2;
                        showCenter();
                        break;
                    case R.id.tab_find:
                        loadFragment(2);
                        checkFlg = 3;
                        break;
                    case R.id.tab_mine:
//                        if (!TextUtils.isEmpty(AppContext.getUserId())) {
                        loadFragment(3);
                        checkFlg = 4;
//                        } else {
//                            Intent intent = new Intent(MainView_Activity.this, LoginActiviy.class);
//                            intent.putExtra(LoginActiviy.FROMACTION, LoginActiviy.MINE);
//                            startActivity(intent);
//                        }
                        break;
                }
            }
        });
        tab_home.setChecked(true);
    }

    private void showCenter() {
        UiHelper.shoSimpleBack(MainActivity.this, SimpleBackPage.DIDIFRAGMENT, null);
    }

    /**
     * 缓存加载
     *
     * @param position
     */
    private static void loadFragment(int position) {
        //从集合中获取相对序号的Fragment
        Fragment f = frags.get(position);
        mTransaction = mManager.beginTransaction();
        if (position == 1) {
            Bundle bundle = new Bundle();
            bundle.putString("type", "0");
            f.setArguments(bundle);
        } else if (position == 2) {
            Bundle bundle = new Bundle();
            bundle.putString("type", "1");
            f.setArguments(bundle);
        }
        //首先判断mFrag 是否为空，如果不为，先隐藏起来，接着判断从List
        // 获取的Fragment是否已经添加到Transaction中，如果未添加，添加后显示，如果已经添加，直接显示
        if (mFrag != null) {
            mTransaction.hide(mFrag);
        }
        if (!f.isAdded()) {
            mTransaction.add(R.id.main_fragment, f);
        } else {
            mTransaction.show(f);
        }
        //将获取的Fragment 赋值给声明的Fragment 中，提交
        mFrag = f;
        mTransaction.commitAllowingStateLoss();
    }

    private long currentTime = 0;

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        if (System.currentTimeMillis() - currentTime > 2000) {
            currentTime = System.currentTimeMillis();
            AppToast.showToast(this, "再次点击将退出应用", "");
        } else {
            MainActivity.this.finish();
            System.exit(0);
        }
    }
}
